package org.example.data_stream;

import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.example.BatchWordCount;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author shenguangyang
 */
public class E01_Source {
    public static void main(String[] args) throws Exception {
        // 1. 创建流试执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 2. 从文件中读取数据
        String filePath = Objects.requireNonNull(BatchWordCount.class.getClassLoader().getResource("clicks.txt")).getPath();
        DataStream<String> stream0 = env.readTextFile(filePath);
        stream0.print("stream0");

        // 3. 从集合中读取数据
        List<Integer> nums = new ArrayList<Integer>() {{
            add(2); add(5);
        }};
        DataStream<Integer> stream1 = env.fromCollection(nums);

        List<Event> events = new ArrayList<Event>() {{
            add(new Event("user01", "./home", 1660095536L));
            add(new Event("user02", "./cart", 1660095536L));
            add(new Event("user01", "./home", 1660095536L));
        }};
        DataStream<Event> stream2 = env.fromCollection(events);

        stream1.print("stream1");
        stream2.print("stream2");

        // 4. 从元素读取数据
        DataStream<Event> stream3 = env.fromElements(
                new Event("user01", "./home", 1660095536L),
                new Event("user02", "./cart", 1660095536L)
        );
        stream3.print("stream3");

        // 5. 从socket中读取数据, linux执行 nc -lk 9000
        DataStream<String> stream4 = env.socketTextStream("work01", 9000);
        stream4.print("stream4");

        env.execute();
    }
}
